# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::wxui::widget::Fleet;
use strict;
use utf8;
use Wx;
use base 'Wx::Panel';
use Wx::Event qw(EVT_BUTTON);
use Wx qw(wxHORIZONTAL wxVERTICAL wxEXPAND wxALL);
use wxui::widget::Units;
use Tools;

sub format_fleet_stats 
{
	my $self = shift;
	my ($space_ships, $carrier_capacity, $transport_capacity, $carried, $transported) = (0, 0, 0, 0, 0);
	foreach ($self->{Fleet}->getAllUnits)
	{
		$space_ships += 1 unless $_->getProp('is_transportable');
		$carrier_capacity += $_->getProp('carrier_capacity');
		$transport_capacity += $_->getProp('transport_capacity');
		$carried += 1 if $_->getProp('is_transportable') == 1;
		$transported += 1 if $_->getProp('is_transportable') == 2;
	};
	return '('.$space_ships.', '.
		($carrier_capacity ? $carried.'/'.$carrier_capacity : '0').', '.
		($transport_capacity ? $transported.'/'.$transport_capacity : '0').')'
}

sub format_alien_fleet_stats
{
	my $self = shift;
	return '('.
		(defined($self->{Fleet}->getProp('turns_till_arrival')) ? $self->{Fleet}->getProp('turns_till_arrival').',' : '').
		$self->{Fleet}->getProp('large_ship_count').','.$self->{Fleet}->getProp('sum_weight').
	')';
}

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $parent = shift;
	my $id = shift;

	my %colors = (
		-1 => Wx::Colour->new(192, 192, 192),
		0 => Wx::Colour->new(255, 0, 0),
		1 => Wx::Colour->new(0, 0, 255),
		2 => Wx::Colour->new(0, 255, 0),
		3 => Wx::Colour->new(0, 255, 255),
		4 => Wx::Colour->new(255, 255, 0)
	);

	my $self = $class->SUPER::new($parent, $id, [-1, -1], [-1, -1]);
	$self->{Fleet} = shift;

	$self->{Sizer} = Wx::BoxSizer->new(wxVERTICAL);

	my $controlled = ref($self->{Fleet}) eq 'dcExplorer::core::Fleet';
	my $harrison = ref($self->{Fleet}) eq 'dcExplorer::core::Harrison';
	
	my $name = 
		(defined($self->{Fleet}->getProp('id')) ? 
			$self->{Fleet}->getProp('id').": " :
			''
		).	
		(defined($self->{Fleet}->getProp('name')) ? 
			$self->{Fleet}->getProp('name') : 
			$harrison ? 'Harrison' : 'Unknown fleet'
		);

	$self->{Header} = Wx::Panel->new($self, -1);
	$self->{Header}->SetBackgroundColour(Wx::Colour->new(0, 0, 0));
	$self->{HeaderSizer} = Wx::BoxSizer->new(wxHORIZONTAL);
	
	$self->{Name} = Wx::StaticText->new($self->{Header}, -1, $name);
	$self->{Name}->SetForegroundColour($self->{Fleet}->getProp('turns_till_arrival') ? Wx::Colour->new(192, 192, 192) : Wx::Colour->new(255, 255, 255));
	$self->{HeaderSizer}->Add($self->{Name}, 0, wxEXPAND, wxALL, 0);

	unless ($harrison)
	{
		$self->{Params} = Wx::StaticText->new($self->{Header}, -1, $controlled ? $self->format_fleet_stats() : $self->format_alien_fleet_stats());
		$self->{Params}->SetForegroundColour(
			$controlled ? 
				$self->{Fleet}->getProp('turns_till_arrival') ? Wx::Colour->new(192, 192, 192) : Wx::Colour->new(255, 255, 255) : 
				defined($self->{Fleet}->getProp('relation_nature')) ? $colors{$self->{Fleet}->getProp('relation_nature')} : Wx::Colour->new(192, 192, 192)
		);
		$self->{HeaderSizer}->Add($self->{Params}, 0, wxEXPAND, wxALL, 0);

		if ($self->{Fleet}->getProp('hidden') || $self->{Fleet}->getProp('fleet_changes'))
		{
			$self->{Hidden} = Wx::StaticText->new($self->{Header}, -1, 'h');
			$self->{Hidden}->SetForegroundColour($self->{Fleet}->getProp('hidden') ? 
					($self->{Fleet}->getProp('fleet_changes') ? 
					 Wx::Colour->new(255, 255, 255) :
					 Wx::Colour->new(192, 192, 192)) : 
					Wx::Colour->new(0, 255, 0));
			my $font = $self->{Hidden}->GetFont();
			$font->SetPointSize($font->GetPointSize() - 1);
			$self->{Hidden}->SetFont($font);
			$self->{HeaderSizer}->Add($self->{Hidden}, 0, wxEXPAND, wxALL, 0);
		}
	}

	$self->{Header}->SetSizer($self->{HeaderSizer});
	$self->{Sizer}->Add($self->{Header}, 0, wxEXPAND, wxALL, 0);

	$self->{Stats} = Wx::Panel->new($self, -1);
	$self->{Stats}->SetBackgroundColour(Wx::Colour->new(0, 0, 0));
	$self->{StatsSizer} = Wx::BoxSizer->new(wxVERTICAL);
	$self->{Location} = Wx::StaticText->new($self->{Stats}, -1, 
		$self->{Fleet}->getProp('turns_till_arrival') ? 
			'Moves: from '.join(':', map {Tools::formatCoordinate($self->{Fleet}->getProp($_))} qw(from_x from_y)).
			' to '.join(':', map {Tools::formatCoordinate($self->{Fleet}->getProp($_))} qw(x y)).
			', turns till arrival: '.$self->{Fleet}->getProp('turns_till_arrival') :
			'Location: '.join(':', map {Tools::formatCoordinate($self->{Fleet}->getProp($_))} qw(x y)));
	$self->{Location}->SetForegroundColour(Wx::Colour->new(255, 255, 255));
	$self->{StatsSizer}->Add($self->{Location}, 0, wxEXPAND, wxALL, 0);
	if (defined $self->{Fleet}->getProp('fly_speed'))
	{
		$self->{Speed} = Wx::StaticText->new($self->{Stats}, -1, sprintf("Speed: %2.3f", $self->{Fleet}->getProp('fly_speed')));
		$self->{Speed}->SetForegroundColour(Wx::Colour->new(255, 255, 255));
		$self->{StatsSizer}->Add($self->{Speed}, 0, wxEXPAND, wxALL, 0);
		$self->{Range} = Wx::StaticText->new($self->{Stats}, -1, sprintf("Range: %2.3f", $self->{Fleet}->getProp('fly_range')));
		$self->{Range}->SetForegroundColour(Wx::Colour->new(255, 255, 255));
		$self->{StatsSizer}->Add($self->{Range}, 0, wxEXPAND, wxALL, 0);
		$self->{Behaviour} = Wx::StaticText->new($self->{Stats}, -1, sprintf("Behaviour: %s", $self->{Fleet}->getProp('behaviour')));
		$self->{Behaviour}->SetForegroundColour(Wx::Colour->new(255, 255, 255));
		$self->{StatsSizer}->Add($self->{Behaviour}, 0, wxEXPAND, wxALL, 0);
	}
	if ($self->{Fleet}->getProp('stealth_lvl'))
	{
		$self->{Stealth} = Wx::StaticText->new($self->{Stats}, -1, sprintf("Stealth: %2.3f", $self->{Fleet}->getProp('stealth_lvl')));
		$self->{Stealth}->SetForegroundColour(Wx::Colour->new(255, 255, 255));
		$self->{StatsSizer}->Add($self->{Stealth}, 0, wxEXPAND, wxALL, 0);
	}
	$self->{Stats}->SetSizer($self->{StatsSizer});
	$self->{Sizer}->Add($self->{Stats}, 0, wxEXPAND, wxALL, 0);
	
	$self->{Units} = dcExplorer::wxui::widget::Units->new($self, -1, [$self->{Fleet}->getAllUnits()]);
	$self->{Sizer}->Add($self->{Units}, 1, wxEXPAND, wxALL, 0);

	$self->{Controls} = Wx::Panel->new($self, -1);
	my @fleets = ();
	push @fleets, [0, 'Harrison'] 
		if $self->{Fleet}->getEmpire()->planets->get(map {$self->{Fleet}->getProp($_)} qw(x y))->getProp('my');
	push @fleets, [$_->getProp('id'), $_->getProp('name')] 
		foreach $self->{Fleet}->getEmpire()->fleets->getFleetsByLocation(map {$self->{Fleet}->getProp($_)} qw(x y)); 

	$self->{ControlsSizer} = Wx::BoxSizer->new(wxHORIZONTAL);
	$self->{MoveButton} = Wx::Button->new($self->{Controls}, -1, "Move to:");
	EVT_BUTTON( $self, $self->{MoveButton}->GetId(), sub { $_[0]->moveTo() } );
	$self->{ControlsSizer}->Add($self->{MoveButton}, 0, wxEXPAND, wxALL, 0);
	$self->{FleetsList} = Wx::Choice->new($self->{Controls}, -1, [-1, -1], [-1, -1], [map {$_->[1]} @fleets]);
	$self->{ControlsSizer}->Add($self->{FleetsList}, 0, wxEXPAND, wxALL, 0);
	$self->{Controls}->SetSizer($self->{ControlsSizer});

	$self->{Sizer}->Add($self->{Controls}, 0, wxEXPAND, wxALL, 0);

	$self->SetSizer($self->{Sizer});

	return $self;
}

sub moveTo
{
	my $self = shift;
}

sub getFleet
{
	my $self = shift;
	return $self->{Fleet};
}

1;
